﻿using StuClassInfo.StuCommon;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StuClassInfo
{
    public partial class ADDstu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlClass();
            }
        }
        #region 绑定班级
        private void ddlClass()
        {
            string sql = "SELECT * FROM dbo.StuClass";
            DataTable table = Dbhelper.datatable(sql);
            dd_class.DataSource = table;
            dd_class.DataTextField = "classname";
            dd_class.DataValueField = "classid";
            dd_class.DataBind();
            dd_class.Items.Insert(0, new ListItem("不限", "0"));


        }
        #endregion
        private void insert()
        {
            UserInfo stu = new UserInfo()
            {
                username = txt_name.Text,
                userage = Convert.ToInt32(txt_age.Text),
                userphone = txt_phone.Text,
                userEmail = txt_email.Text,
                classid = Convert.ToInt32(dd_class.SelectedValue)
            };
            //1.实例化
            var db = new StuUseEntities();
            db.UserInfo.Add(stu);
            if (db.SaveChanges() > 0)
            {
                JSHelper.Alert(this, "添加成功", "StuInfo.aspx");
            }
            else
            {
                JSHelper.Alert(this, "添加失败", "StuInfo.aspx");
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            insert();
        }
    }
}